﻿using Client.ICommandNotify;
using Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.CBMDataSelection
{
    public class CBMDataViewModel: NotifyBase
    {
        /// <summary>
        /// 
        /// </summary>
        public CBMDataViewModel(List<GcjsbDtlCBM> GcjsbDtlCBMListSource)
        {
            this.GcjsbDtlCBMListSource= GcjsbDtlCBMListSource;
            GcjsbDtlSource = GcjsbDtlCBMListSource;
            SearchCommand = new Command(SearchCommandDoExecute, SearchCommandCanExecute);
            UpdateCbm = new Command(UpdateCbmDoExecute, UpdateCbmCanExecute);
        }

        private void UpdateCbmDoExecute(object obj)
        {
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            var DbContext_ = HelpTools.IOC.Getobject<SqlServer.DbContext>();
            DbContext_.SqlConfigstr = "Data Source=192.168.100.6;Initial Catalog=SRBG_CBM;User ID=sa;Password=asdw@122";
            var ListData=  GcjsbDtlCBMListSource.Where(o=> Decimal.Parse(string.IsNullOrWhiteSpace(o.ResidueAmount) ? "0" : o.ResidueAmount) < 0);
            foreach (var item in ListData)
            {
                DbContext_.Update($"UPDATE CBM_FBContractList SET SettledAmount={item.LeiJiAmount},SettledQty={item.LeiJiQty} WHERE DtlId='{item.DtlId}'");
            }
        }

        private bool UpdateCbmCanExecute()
        {
            return true;
        }

        private bool SearchCommandCanExecute()
        {
            return true;
        }

        #region 命令

        public Command SearchCommand { get; set; }
        public Command UpdateCbm { get; set; }
        #endregion
        #region 方法


        private void SearchCommandDoExecute(object obj)
        {
            if (GcjsbDtlSource == null) GcjsbDtlSource = new List<GcjsbDtlCBM>();

            if(string.IsNullOrWhiteSpace(obj as string)){
                if(GcjsbDtlCBMListSource.Count!= GcjsbDtlSource.Count)
                GcjsbDtlCBMListSource = GcjsbDtlSource;
            }
            else
            {
                GcjsbDtlCBMListSource = GcjsbDtlSource.Where(o => o.XmCode.Contains(obj as string)).ToList();
            }
          
        }
        #endregion

        private string SearchText_;

        public string SearchText
        {
            get { return SearchText_; }
            set { SearchText_ = value; }
        }
        private List<GcjsbDtlCBM> GcjsbDtlSource=new List<GcjsbDtlCBM>();
        private List<GcjsbDtlCBM> GcjsbDtlCBMListSource_;

        public List<GcjsbDtlCBM> GcjsbDtlCBMListSource
        {
            get { return GcjsbDtlCBMListSource_; }
            set { GcjsbDtlCBMListSource_ = value;
                SetProperty(ref GcjsbDtlCBMListSource_, GcjsbDtlCBMListSource);
            }
        }

    }
}
